﻿#region Using directives

using System;
using System.Xml;
using System.Drawing.Design;
using System.ComponentModel;
using MCMLXVII.BackEnd.Services.OrganizationChart.Messages;
using MCMLXVII.BackEnd.Services.OrganizationChart.Interfaces;
using MCMLXVII.Core.Common;

#endregion

namespace MCMLXVII.Tools.OrganizationChart.MembersClasses
{

    #region TypeConverters

    public class DirectoryConverter : StringConverter
    {
        public override bool GetStandardValuesSupported(
                               ITypeDescriptorContext context)
        {
            return true;
        }
        public override StandardValuesCollection
                     GetStandardValues(ITypeDescriptorContext context)
        {
            return new StandardValuesCollection(MemberBase.DirectoryServicesNames());
        }

        public override bool GetStandardValuesExclusive(
                               ITypeDescriptorContext context)
        {
            return true;
        }

        public static int GetSelectedItemID(string item)
        {
            string[] DirectoryServices = MemberBase.DirectoryServicesNames();
            for (int i = 0; i < DirectoryServices.Length ; i++)
            {
                if (DirectoryServices[i] == item)
                    return MemberBase.DirectoryServicesIDs()[i];
            }
            return 0;
        }

        public static string GetSelectedItemDescription(int item)
        {
            int[] DirectoryServicesIDs = MemberBase.DirectoryServicesIDs();
            for (int i = 0; i < DirectoryServicesIDs.Length; i++)
            {
                if (DirectoryServicesIDs[i] == item)
                    return MemberBase.DirectoryServicesNames()[i];
            }
            return Properties.Resources.NotFound;
        }
    }
    public class LanguageConverter : StringConverter
    {

        public override bool GetStandardValuesSupported(
                               ITypeDescriptorContext context)
        {
            return true;
        }

        public override StandardValuesCollection
                     GetStandardValues(ITypeDescriptorContext context)
        {
            return new StandardValuesCollection(MemberBase.ContentsLanguageList());
        }

        public override bool GetStandardValuesExclusive(
                               ITypeDescriptorContext context)
        {
            return true;
        }

    }

    #endregion

    #region UITypeEditors

    public class DirectoryServiceUserEditor : UITypeEditor
    {
        public DirectoryServiceUserEditor()
        { }


        public override bool GetPaintValueSupported(ITypeDescriptorContext context)
        {
            return false;
        }

        public override void PaintValue(PaintValueEventArgs e)
        {
        }

        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.Modal;
        }

        public override bool IsDropDownResizable
        {
            get { return false; }
        }

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            frmDirectoryServiceUsers frmDialog = new frmDirectoryServiceUsers((string)value, ((MemberUser)context.Instance).DirectoryIdentifier, ((MemberUser)context.Instance).SecurityProviderID, ((MemberUser)context.Instance).SecurityProvider, true);
            System.Windows.Forms.DialogResult res = frmDialog.ShowDialog(); //Program.frm);
            if (res == System.Windows.Forms.DialogResult.Cancel)
                return value;
            else
            {
                ((MemberUser)context.Instance).DirectoryIdentifier = frmDialog.ID;
                foreach (XmlNode node in frmDialog.PropertyMaps)
                {
                    string NewValue = node.Attributes.GetNamedItem("Value").Value;
                    switch (node.Attributes.GetNamedItem("Name").Value.ToLower())
                    {
                        case "language": ((MemberUser)context.Instance).Language = NewValue;
                            break;
                        case "description": ((MemberUser)context.Instance).Description = NewValue;
                            break;
                        default:
                            // User Defined Properties
                            // AACCAA
                            break;
                    }

                }
                return frmDialog.LogonName;
            }
        }
    }
    public class UserGroupMembershipEditor : UITypeEditor
    {
        public UserGroupMembershipEditor()
        { }

        public override string ToString()
        {
            return Properties.Resources.Collection;
        }

        public override bool GetPaintValueSupported(ITypeDescriptorContext context)
        {
            return false;
        }

        public override void PaintValue(PaintValueEventArgs pe)
        {
            //pe.Graphics.DrawString(Properties.Resources.Collection, ((System.Windows.Forms.PropertyGrid)pe.Context.Container).Font, System.Drawing.Brushes.Cyan, pe.Bounds);
        }

        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.Modal;
        }

        public override bool IsDropDownResizable
        {
            get { return false; }
        }

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            //((MemberUser)context.Instance).MemberOf
            frmMembersEditor frmDialog = new frmMembersEditor((msgMembershipInfo[])value, ((MemberBase)context.Instance).MemberClass, ((MemberBase)context.Instance).Description);
            System.Windows.Forms.DialogResult res = frmDialog.ShowDialog(); //Program.frm);
            if (res == System.Windows.Forms.DialogResult.Cancel)
                return value;
            else
                return frmDialog.ActualMembers;
        }
    }
    public class DirectoryServiceGroupEditor : UITypeEditor
    {
        public DirectoryServiceGroupEditor()
        { }


        public override bool GetPaintValueSupported(ITypeDescriptorContext context)
        {
            return false;
        }

        public override void PaintValue(PaintValueEventArgs e)
        {
        }

        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.Modal;
        }

        public override bool IsDropDownResizable
        {
            get { return false; }
        }

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            frmDirectoryServiceUsers frmDialog = new frmDirectoryServiceUsers((string)value, ((MemberSecurityGroup)context.Instance).DirectoryIdentifier, ((MemberSecurityGroup)context.Instance).SecurityProviderID, ((MemberSecurityGroup)context.Instance).SecurityProvider, false);
            System.Windows.Forms.DialogResult res = frmDialog.ShowDialog(); //Program.frm);
            if (res == System.Windows.Forms.DialogResult.Cancel)
                return value;
            else
            {
                ((MemberSecurityGroup)context.Instance).DirectoryIdentifier = frmDialog.ID;
                foreach (XmlNode node in frmDialog.PropertyMaps)
                {
                    string NewValue = node.Attributes.GetNamedItem("Value").Value;
                    switch (node.Attributes.GetNamedItem("Name").Value.ToLower())
                    {
                        case "description": ((MemberSecurityGroup)context.Instance).Description = NewValue;
                            break;
                        default:
                            // User Defined Properties
                            // AACCAA
                            break;
                    }

                }
                return frmDialog.LogonName;
            }
        }
    }
    public class RoleMembershipEditor : UITypeEditor
    {
        public RoleMembershipEditor()
        { }

        public override string ToString()
        {
            return Properties.Resources.Collection;
        }

        public override bool GetPaintValueSupported(ITypeDescriptorContext context)
        {
            return false;
        }

        public override void PaintValue(PaintValueEventArgs pe)
        {
            //pe.Graphics.DrawString(Properties.Resources.Collection, ((System.Windows.Forms.PropertyGrid)pe.Context.Container).Font, System.Drawing.Brushes.Cyan, pe.Bounds);
        }

        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.Modal;
        }

        public override bool IsDropDownResizable
        {
            get { return false; }
        }

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            //((MemberUser)context.Instance).MemberOf
            frmMembersSelector frmDialog = new frmMembersSelector((msgMembershipInfo)value, ((MemberBase)context.Instance).MemberClass, ((MemberBase)context.Instance).Description);
            System.Windows.Forms.DialogResult res = frmDialog.ShowDialog(); //Program.frm);
            if (res == System.Windows.Forms.DialogResult.Cancel)
                return value;
            else
                return frmDialog.ActualMember;
        }
    }


    public class OwnerEditor : UITypeEditor
    {
        public OwnerEditor()
        { }

        public override string ToString()
        {
            return Properties.Resources.Collection;
        }

        public override bool GetPaintValueSupported(ITypeDescriptorContext context)
        {
            return false;
        }

        public override void PaintValue(PaintValueEventArgs pe)
        {
            //pe.Graphics.DrawString(Properties.Resources.Collection, ((System.Windows.Forms.PropertyGrid)pe.Context.Container).Font, System.Drawing.Brushes.Cyan, pe.Bounds);
        }

        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.Modal;
        }

        public override bool IsDropDownResizable
        {
            get { return false; }
        }

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            //((MemberUser)context.Instance).MemberOf
            frmMembersSelector frmDialog = new frmMembersSelector((msgMembershipInfo)value, 0, ((NodeEditorHelper)context.Instance).Name);
            System.Windows.Forms.DialogResult res = frmDialog.ShowDialog(); //Program.frm);
            if (res == System.Windows.Forms.DialogResult.Cancel)
                return value;
            else
                return frmDialog.ActualMember;
        }
    }



    #endregion

}
